﻿using FarPoint.Web.Spread;
using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.AjaxSupport
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, System.EventArgs e)
    {
      if (this.IsPostBack)
        return;
      this.FpSpread1.UseClipboard = false;
      FpSpread1.ActiveSheetView.RowCount = 30;
      FpSpread1.ActiveSheetView.ColumnCount = 10;
      FpSpread1.ActiveSheetView.PageSize = 15;
      FpSpread1.Sheets.Count = 3;

      FpSpread1.ScrollBarBaseColor = Color.LightSteelBlue;

      for (int i = 0; i < FpSpread1.Sheets.Count; i++)
        ApplySkin(FpSpread1.Sheets[i]);

      FpSpread1.CommandBar.Visible = false;
      FpSpread1.EnableAjaxCall = true;
      FpSpread1.ClientAutoCalculation = true;

    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
      //
      // CODEGEN: This call is required by the ASP.NET Web Form Designer.
      //
      InitializeComponent();
      base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.FpSpread1.CreateButton += new FarPoint.Web.Spread.CreateButtonEventHandler(this.FpSpread1_CreateButton);
      this.FpSpread1.ButtonCommand += new FarPoint.Web.Spread.SpreadCommandEventHandler(this.FpSpread1_ButtonCommand);

    }
    #endregion

    private void ApplySkin(FarPoint.Web.Spread.SheetView sh)
    {
      sh.HeaderGrayAreaColor = Color.LightSteelBlue;

      FarPoint.Web.Spread.LabelCellType ch = new FarPoint.Web.Spread.LabelCellType();
      ch.BackgroundImageUrl = @"../images/ajax/toolbar.Horizontal.background.gif";
      sh.ColumnHeader.DefaultStyle.CellType = ch;

      sh.SheetCornerStyle.CellType = ch;

      FarPoint.Web.Spread.LabelCellType rh = new FarPoint.Web.Spread.LabelCellType();
      rh.BackgroundImageUrl = @"../images/ajax/bg.bmp";
      sh.RowHeader.DefaultStyle.CellType = rh;

      sh.RowCount = 10;
      //Add text
      sh.Cells[2, 1].Text = "费用";
      sh.Cells[3, 1].Text = "税金";
      sh.Cells[4, 1].Text = "小计";
      sh.Cells[5, 1].Text = "合计";

      sh.Cells[5, 1].ForeColor = Color.Red;
      sh.Cells[5, 1].HorizontalAlign = HorizontalAlign.Right;

      sh.Columns[2].HorizontalAlign = HorizontalAlign.Right;
      sh.Cells[2, 2].CellType = new CurrencyCellType();
      sh.Cells[2, 2].Value = 8;
      sh.Cells[3, 2].CellType = new PercentCellType();
      sh.Cells[3, 2].Value = 0.6;
      sh.Cells[4, 2].Formula = "C3*C4";
      sh.Cells[5, 2].Formula = "SUM(C3,C5)";

    }

    private void FpSpread1_ButtonCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
    {
      int n;
      int size;
      Color bc;

      if (e.CommandName.Equals("FontBold"))
        this.FontBold();
      else if (e.CommandName.Equals("FontItalic"))
        this.FontItalic();
      else if (e.CommandName.Equals("FontUnderline"))
        this.FontUnderline();
      else if (e.CommandName.StartsWith("FontName"))
      {
        n = e.CommandName.IndexOf(".") + 1;
        this.FontName(e.CommandName.Substring(n));
      }
      else if (e.CommandName.StartsWith("FontSize"))
      {
        n = e.CommandName.IndexOf(".") + 1;
        size = int.Parse(e.CommandName.Substring(n));
        this.FontSize(size);
      }
      else if (e.CommandName.StartsWith("BackColor"))
      {
        n = e.CommandName.IndexOf(".") + 1;
        bc = Color.FromName(e.CommandName.Substring(n));
        this.BackColor(bc);
      }
      else if (e.CommandName.StartsWith("ForeColor"))
      {
        n = e.CommandName.IndexOf(".") + 1;
        bc = Color.FromName(e.CommandName.Substring(n));
        this.ForeColor(bc);
      }
      else if (e.CommandName.Equals("AlignLeft"))
        this.AlignLeft();
      else if (e.CommandName.Equals("AlignCenter"))
        this.AlignCenter();
      else if (e.CommandName.Equals("AlignRight"))
        this.AlignRight();
    }

    private void FontBold()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      FontInfo f = FpSpread1.ActiveSheetView.Cells[r, c].Font;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        f.Bold = !f.Bold;
      else
      {
        FarPoint.Web.Spread.StyleInfo si = new FarPoint.Web.Spread.StyleInfo();
        si.Font.CopyFrom(FpSpread1.ActiveSheetView.Cells[r, c].Font);
        si.Font.Bold = !f.Bold;

        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].Font = si.Font;
      }
    }

    private void FontItalic()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      FontInfo f = FpSpread1.ActiveSheetView.Cells[r, c].Font;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        f.Italic = !f.Italic;
      else
      {
        FarPoint.Web.Spread.StyleInfo si = new FarPoint.Web.Spread.StyleInfo();
        si.Font.CopyFrom(FpSpread1.ActiveSheetView.Cells[r, c].Font);
        si.Font.Italic = !f.Italic;

        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].Font = si.Font;
      }
    }

    private void FontUnderline()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      FontInfo f = FpSpread1.ActiveSheetView.Cells[r, c].Font;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        f.Underline = !f.Underline;
      else
      {
        FarPoint.Web.Spread.StyleInfo si = new FarPoint.Web.Spread.StyleInfo();
        si.Font.CopyFrom(FpSpread1.ActiveSheetView.Cells[r, c].Font);
        si.Font.Underline = !f.Underline;

        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].Font = si.Font;
      }
    }

    private void FontName(string name)
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      FontInfo f = FpSpread1.ActiveSheetView.Cells[r, c].Font;
      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        f.Name = name;
      else
      {
        FarPoint.Web.Spread.StyleInfo si = new FarPoint.Web.Spread.StyleInfo();
        si.Font.CopyFrom(FpSpread1.ActiveSheetView.Cells[r, c].Font);
        si.Font.Name = name;

        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].Font = si.Font;
      }
    }

    private void FontSize(int size)
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      FarPoint.Web.Spread.StyleInfo si = new FarPoint.Web.Spread.StyleInfo();

      FontInfo f = FpSpread1.ActiveSheetView.StyleModel.GetCompositeInfo(r, c, -1, null).Font;

      int s1 = size;
      switch (s1)
      {
        case 1:
          f.Size = FontUnit.Smaller;
          break;
        case 2:
          f.Size = FontUnit.Small;
          break;
        case 3:
          f.Size = FontUnit.Medium;
          break;
        case 4:
          f.Size = FontUnit.Larger;
          break;
        case 5:
          f.Size = FontUnit.Large;
          break;
        case 6:
          f.Size = FontUnit.XLarge;
          break;
      }

      if (FpSpread1.ActiveSheetView.SelectionModel.Count > 0)
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].Font = f;
      }
    }

    private void BackColor(Color backcolor)
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        FpSpread1.ActiveSheetView.Cells[r, c].BackColor = backcolor;
      else
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].BackColor = backcolor;
      }
    }

    private void ForeColor(Color forecolor)
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        FpSpread1.ActiveSheetView.Cells[r, c].ForeColor = forecolor;
      else
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].ForeColor = forecolor;
      }
    }

    private void AlignLeft()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        FpSpread1.ActiveSheetView.Cells[r, c].HorizontalAlign = HorizontalAlign.Left;
      else
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].HorizontalAlign = HorizontalAlign.Left;
      }
    }

    private void AlignCenter()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        FpSpread1.ActiveSheetView.Cells[r, c].HorizontalAlign = HorizontalAlign.Center;
      else
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].HorizontalAlign = HorizontalAlign.Center;
      }
    }

    private void AlignRight()
    {
      int r = FpSpread1.ActiveSheetView.ActiveRow;
      int c = FpSpread1.ActiveSheetView.ActiveColumn;

      if (FpSpread1.ActiveSheetView.SelectionModel.Count == 0)
        FpSpread1.ActiveSheetView.Cells[r, c].HorizontalAlign = HorizontalAlign.Right;
      else
      {
        FarPoint.Web.Spread.Model.CellRange cr = FpSpread1.ActiveSheetView.SelectionModel[0];
        FpSpread1.ActiveSheetView.Cells[cr.Row, cr.Column, cr.Row + cr.RowCount - 1, cr.Column + cr.ColumnCount - 1].HorizontalAlign = HorizontalAlign.Right;
      }
    }

    private void FpSpread1_CreateButton(object sender, FarPoint.Web.Spread.CreateButtonEventArgs e)
    {
      if (e.Command == "Cut" || e.Command == "Copy" || e.Command == "Paste")
        e.Visible = false;
    }

    protected override void Render(HtmlTextWriter writer)
    {
      WebControl cmdbar = (WebControl)FpSpread1.FindControl("commandBar");
      if (cmdbar != null)     
        cmdbar.Attributes.CssStyle.Add("background-image", "url(../images/ajax/toolbar.Horizontal.background.gif)");
      WebControl cmdbarTbl = (WebControl)FpSpread1.FindControl("cmdTable");
      if (cmdbarTbl != null)
        cmdbarTbl.Attributes.CssStyle.Add("height", "27px");     
      base.Render(writer);
    }
   
  }
}